// You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
// Output: 7 -> 0 -> 8

var addTwoNumbers = function(l1, l2) {
    	var link1 = new Link(l1),
    		link2 = new Link(l2),
    		link = new Link(),
    		p = link1.head,
    		q = link2.head,
    		n = link.head;
    	while(p.next && q.next) {
    		var value = p.next.add(q.next);
    		n.next = new Point(value);
    		p = p.next;
    		q = q.next;
    		n = n.next;
    		// console.log(n.value)
    	}
    	return link.toString();
	};

	var Point = function(v,n) {
		this.value = v || 0;
		this.next = n || null;
	}
	Point.prototype.add = function(n){
		var sum =  parseInt(this.value) + parseInt(n.value);
		if(sum >= 10) {
			return sum - 10;
		}else {
			return sum;
		}
	}

	var Link = function(arr){
		this.head = new Point(null,this.init(arr));

	}
	Link.prototype.init = function(arr) {
		if(arr == null || arr.length === 0) {
			return null;
		}
		var len = arr.length,
			nodes = [],
			i,
			n;
		for(i = 0; i < len; i++) {
			nodes.push(new Point(arr[i]));
		}
		for(n = 0; n < len - 1; n++) {
			nodes[n].next = nodes[n + 1];
		}
		return nodes[0];
	};
	Link.prototype.toString = function() {
		var str = '',
			p = this.head;
		if(p.next) {
			str += p.next.value;
			p = p.next
		}
		while(p.next) {
			str += ' -> ' + p.next.value;
			p = p.next
		}
		return str;
	}